package exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import util.ErrorCode;
import util.ResponseUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @author GeChen
 * @Description 全局异常处理器
 * @date 2020/6/16 17:15
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
        e.printStackTrace();
        if (e instanceof GlobalException) {
            GlobalException globalException = (GlobalException) e;
            return ResponseUtil.getResponseString(ErrorCode.SERVER_ERROR, globalException.getCodeMsg(), "");
        } else {
            return ResponseUtil.getResponseString(ErrorCode.SERVER_ERROR, "服务端错误", "");
        }
    }
}
